#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<time.h>
#include<Windows.h>
#include<vector>
#include<algorithm>
#include "bid_list.h"
#include<cassert>
using namespace std;


LTNode* LTInit()
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	assert(node);

	node->data = -1;
	node->next = node;
	node->pre = node;

	return node;
}

void LTDestroy(LTNode* phead)
{
	LTNode* tem = phead->next;
	LTNode* de = tem->next;

	while (tem != phead)
	{
		free(tem);
		tem = de;
		de = de->next;
	}

	free(phead);
}

void LTPrint(LTNode* phead)
{
	LTNode* tem = phead->next;
	while (tem != phead)
	{
		printf("%d ", tem->data);
		tem = tem->next;
		Sleep(500);
	}
	
	printf("\n");
}

bool LTEmpty(LTNode* phead)
{
	if (phead->next == phead)
	{
		return true;
	}
	return false;
}

void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* node = LTInit();
	node->data = x;

	LTNode* tail = phead->pre;

	phead->pre = node;

	tail->next = node;

	node->pre = tail;
	node->next = phead;
}

void LTPopBack(LTNode* phead)
{
	assert(phead);

	if (LTEmpty(phead))
	{
		return;
	}

	LTNode* newtail = phead->pre->pre;

	LTNode* tail = phead->pre;

	newtail->next = phead;

	phead->pre = newtail;

	free(tail);

}

void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* newhead = LTInit();
	newhead->data = x;

	LTNode* head = phead->next;

	newhead->pre = phead;
	newhead->next = head;

	phead->next = newhead;

	head->pre = newhead;
}

void LTPopFront(LTNode* phead)
{
	assert(phead);
	if (LTEmpty(phead))
	{
		return;
	}

	LTNode* newhead = phead->next->next;
	LTNode* oldhead = phead->next;

	phead->next = newhead;
	newhead->pre = phead;

	free(oldhead);

}

void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* node = LTInit();
	node->data = x;

	LTNode* prev = pos->pre;

	prev->next = node;
	node->pre = prev;

	node->next = pos;
	pos->pre = node;
}

void LTErase(LTNode* pos)
{
	assert(pos);

	LTNode* prev = pos->pre;
	LTNode* back = pos->next;

	prev->next = back;
	back->pre = prev;

	free(pos);
}

LTNode* LTFind(LTNode* phead, LTDataType x)
{
	LTNode* tem = phead->next;

	while (tem != phead)
	{
		if (tem->data == x)
		{
			return tem;
		}

		tem = tem->next;
	}
	return nullptr;
}
